package com.xquant.trade.trade.service.impl.otcOption;

import com.jayway.jsonpath.JsonPath;
import com.xquant.trade.account.entity.Account;
import com.xquant.trade.trade.entity.Trade;
import com.xquant.trade.trade.entity.TradeDetileDto;
import com.xquant.trade.trade.service.RenderExporter;
import com.xquant.trade.utils.DateUtils;
import com.xquant.trade.utils.Instrument;
import com.xquant.trade.utils.POIWordUtil;
import net.minidev.json.JSONArray;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @Author: xpp -_- -_-
 * @Date: 2021/8/4 14:09
 */
@Component
public class AutoCallRenderExportor extends RenderExporter {

    @Override
    public Map RenderData(TradeDetileDto tradeDetile) {
        Map map = new HashMap();
        Trade trade = tradeDetile.getTrade();
        Map instrumentInfo = trade.getInstrumentInfo();
        String pCalc = instrumentInfo.get("pCalc").toString();
        LinkedHashMap termsheet = JsonPath.read(pCalc, "$.termsheet");
        JSONArray underlyerIds = null;
        if (null != termsheet.get("underlyerIds")) {
            underlyerIds = (JSONArray) termsheet.get("underlyerIds");
        }
        String setDate = trade.getSetDate();//计算日期
        map.put("setDate", DateUtils.formatChineseDate(setDate));//期权结算日
        map.put("effectiveDate", DateUtils.formatChineseDate(termsheet.get("effectiveDate")));//合约起始日
        map.put("terminationDate", DateUtils.formatChineseDate(termsheet.get("terminationDate")));//合约到期日
        LinkedHashMap<String, Object> knockOut = (LinkedHashMap<String, Object>) termsheet.get("knockOut");
        JSONArray knocks = (JSONArray) knockOut.get("knocks");
        //敲出收益条款
        ArrayList<ArrayList<String>> rows = new ArrayList<>();
        for (Object knock : knocks) {
            Object paymentDate = ((LinkedHashMap) knock).get("paymentDate");
            ArrayList<String> strings = new ArrayList<>();
            strings.add(paymentDate.toString());
            rows.add(strings);
        }
        //观察日列表
        map.put("ll",POIWordUtil.generateTable(rows));
        map.put("paymentDate", trade.getTradeId());//期权费支付日
        map.put("amount", trade.getAmount());//期权费
        String payDirection = knockOut.get("payDirection").toString()=="1"?"买入":"卖出";//买卖方向ss
        map.put("payDirection",payDirection);
        map.put("issuePrice", termsheet.get("issuePrice"));//期初价格
        LinkedHashMap extInfo = JsonPath.read(trade.getExtInfo(), "$");
        map.put("tradeMarket",extInfo.get("tradeMarket") );//交易所
        Instrument instruments = tradeDetile.getInstruments();
        map.put("iCode", instruments.getiCode());
        map.put("aType", instruments.getaType());
        map.put("mType", instruments.getmType());
        map.put("iName", instruments.getiName());
        //名义本金
        map.put("notional", termsheet.get("notional"));
        //期权卖方
        map.put("tradeId", trade.getTradeId());
        map.put("accNameCp", trade.getAccNameCp());
        map.put("accName", trade.getAccName());
        map.put("instrumentInfo", instrumentInfo.get("pCalc").toString());
        map.put("ExtInfo", trade.getExtInfo());
        //查询交易双方的信息
        Account account = tradeDetile.getAccount();//本方账户
        Account cpAccount = tradeDetile.getCpAccount();//对手方账户
        String cpAextInfo = cpAccount.getExtInfo();//对手方账户附加信息
        JSONArray banks = JsonPath.read(cpAextInfo, "$.banks");
        if (banks.size() > 0) {
            LinkedHashMap bank = (LinkedHashMap) banks.get(0);
            Object zh = bank.get("zh");
            Object hm = bank.get("hm");
            Object khh = bank.get("khh");
            Object zfxthh = bank.get("zfxthh");
            map.put("zh", zh);
            map.put("hm", hm);
            map.put("khh", khh);
            map.put("zfxthh", zfxthh);
        }

        return map;
    }

    @Override
    public TradeDetileDto getTradeDetile(Map params) {
        return tradeMapper.queryTrade4OtcOptionForDetile(params);
    }
}
